//
//  ByBallBetDetailVC.m
//  caipiao_ios
//
//  Created by mc on 2018/8/14.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "ByBallBetDetailVC.h"
#import "ByBetRecordTitleCell.h"
#import "ByBetRecordInfoCell.h"
#import "ByBetRecordromptCell.h"
#import "ByBallBetInfoCell.h"
#import "ByFootBallBetVC.h"
#import "ByTabBarVC.h"
#import "ByBasketBallBetVC.h"

@interface ByBallBetDetailVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) ByNetModel *infoModel;

@end

@implementation ByBallBetDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addLeftBackBarButtonItem];
    self.title = @"投注详情";
    [self loadData];
    
   
    // Do any additional setup after loading the view.
}

- (void)loadData{
    [ByCoreToolAlert showLoadind:@"加载中" hasMasker:YES];
    ByNetModel *netModel1 = [[ByNetModel alloc] init];
    netModel1.ID = self.netModel.ID;
    [ByApiOther betLogSportsDetail:netModel1 success:^(ByNetBaseResult *result) {
        [ByCoreToolAlert hideHud];
         [self initViews];
        SLog(@"足球投注详情 = %@", result.data);
        NSDictionary *dictionary = result.data[@"data"];
        self.infoModel = [[ByNetModel alloc] initContentWithDic:dictionary[@"info"]];
        
        if (self.netModel.gamecate.integerValue ==  3010) {
            // 足球
            NSMutableDictionary *dic1 =  [ByFootBallHandle getFootBallGameResultWithPlan:self.infoModel.origin_num];
            NSMutableArray *dataArray = [NSMutableArray arrayWithCapacity:1];
            for (NSDictionary *dic in dictionary[@"note_record"]) {
                ByNetModel *netModel = [[ByNetModel alloc] initContentWithDic:dic[@"qiudui"]];
                netModel.bet_Content = dic1[netModel.race_id];
                netModel.game_Result = [ByFootBallHandle getGameResultStringWithSaiDic:dic[@"sai"]];
                [dataArray addObject:netModel];
            }
            self.infoModel.dataArray = [dataArray copy];
            
        }else{
            // 篮球
            
            NSMutableDictionary *dic1 =  [ByBasketBallHandle getBasketBallGameResultWithPlan:self.infoModel.origin_num];
            NSMutableArray *dataArray = [NSMutableArray arrayWithCapacity:1];
            for (NSDictionary *dic in dictionary[@"note_record"]) {
                ByNetModel *netModel = [[ByNetModel alloc] initContentWithDic:dic[@"qiudui"]];
                netModel.bet_Content = dic1[netModel.race_id];
                netModel.game_Result = [ByBasketBallHandle getBasketBallGameResultStringWithSaiDic:dic[@"sai"]];
                netModel.rq = [ByBasketBallHandle getBasketBallGameWithSaiDicRq:dic[@"sai"]];
                
                [dataArray addObject:netModel];
            }
            self.infoModel.dataArray = [dataArray copy];
            
        }
        
        
        [self.tableView reloadData];
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showFailureWithText:result.msg];
    }];
    
}


-(void)initViews{
    
    self.view.backgroundColor = BY_BACKGROUND_COLOR;
    self.tableView = [[UITableView alloc] init];
    self.tableView.backgroundColor = BY_BACKGROUND_COLOR;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        if (@available(ios 11.0,*)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(0);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-49);
        } else {
            make.top.equalTo(self.view).offset(0);
            make.bottom.equalTo(self.view).offset(-49);
        }
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    UIButton *betBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:betBtn];
    [betBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
       
        make.height.mas_equalTo(@49);
        if (@available(ios 11.0,*)) {
             make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(0);
        } else {
             make.bottom.equalTo(self.view).offset(0);
        }
    }];
    betBtn.backgroundColor = BY_THEME_COLOR;
    [betBtn setTitle:@"继续投注" forState:UIControlStateNormal];
    [betBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [betBtn addTarget:self action:@selector(betAction:) forControlEvents:UIControlEventTouchUpInside];
    
}


- (void)betAction:(UIButton *)btn{
    
    if (![BY_APPDELEGATE.supportTypeString containsString:[NSString stringWithFormat:@"%@", self.netModel.gamecate]]) {
        [ByCoreToolAlert showOnlyText:@"暂不支持"];
        return ;
    }
    if (self.netModel.gamecate.integerValue == 3010){
        
        ByFootBallBetVC *vc = [[ByFootBallBetVC alloc] init];
        [self by_normalPush:vc hasNavi:YES];
        
    }else if (self.netModel.gamecate.integerValue == 3001){
        
        ByBasketBallBetVC *vc = [[ByBasketBallBetVC alloc] init];
        [self by_normalPush:vc hasNavi:YES];
        
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        ByBetRecordTitleCell *cell = [ByBetRecordTitleCell cellWithTableView:tableView];
        cell.netModel = self.infoModel;
        return cell;
    }else if (indexPath.row == 1){
        
        ByBallBetInfoCell *cell = [ByBallBetInfoCell cellWithTableView:tableView];
        cell.netModel = self.infoModel;
        return cell;
        
    }else if (indexPath.row == 2){
        ByBetRecordromptCell *cell = [ByBetRecordromptCell cellWithTableView:tableView];
        return cell;
    }
    
    static NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        return 90;
    }else if (indexPath.row == 1){
        
        CGFloat top = 105 + 40 + self.infoModel.dataArray.count * Table_Height + 10;
        return top + 30 * 7 + 20;
        
    }else{
        
        return 180;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
